package com.kotoframework.onedoc.utils

import com.aspose.cells.PdfCompliance
import com.aspose.cells.PdfOptimizationType
import com.aspose.cells.PdfSaveOptions
import com.aspose.cells.Workbook
import com.aspose.slides.Presentation
import com.aspose.words.Document
import com.leinbo.onedoc.utils.LogUtils
import org.apache.pdfbox.Loader
import org.apache.pdfbox.rendering.ImageType
import org.apache.pdfbox.rendering.PDFRenderer
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import java.awt.image.BufferedImage
import java.io.File


/**
 * Created by sundaiyue on 2022/11/28 01:29
 */
object TransformUtil {
    private val cellOptions = PdfSaveOptions().apply {
        allColumnsInOnePagePerSheet = true
        onePagePerSheet = true
        compliance = PdfCompliance.PDF_A_1_B
        optimizationType = PdfOptimizationType.MINIMUM_SIZE
    }

    val wordOptions = com.aspose.words.SaveOptions.createSaveOptions(com.aspose.words.SaveFormat.PDF)

    fun excel2Pdf(file: File): String {
        val old = System.currentTimeMillis()
        val pdfPath = file.absolutePath.replace(file.extension, "pdf")
        try {
            timedTask("EXCEL 转 Pdf") {
                val workbook = Workbook(file.absolutePath)
                workbook.save(pdfPath, cellOptions)
            }
        } catch (e: Exception) {
            LogUtils.log("EXCEL 转 Pdf失败", e)
        }
        return pdfPath
    }

    fun excel2Htm(file: File): String {
        val old = System.currentTimeMillis()
        val htmlPath = file.absolutePath.replace(file.extension, "html")
        try {
            timedTask("EXCEL 转 Html") {
                val workbook = Workbook(file.absolutePath)
                workbook.save(htmlPath, com.aspose.cells.SaveFormat.HTML)
            }
        } catch (e: Exception) {
            LogUtils.log("EXCEL 转 Html失败", e)
        }
        return htmlPath
    }

    fun word2Pdf(file: File): String {
        val old = System.currentTimeMillis()
        val pdfPath = file.absolutePath.replace(file.extension, "pdf")
        try {
            timedTask("WORD 转 Pdf") {
                val document = Document(file.absolutePath)
                document.save(pdfPath, wordOptions)
            }
        } catch (e: Exception) {
            LogUtils.log("WORD 转 Pdf失败", e)
        }
        return pdfPath
    }

    fun ppt2Pdf(file: File): String {
        val pdfPath = file.absolutePath.replace(file.extension, "pdf")
        try {
            timedTask("PPT 转 Pdf") {
                val presentation = Presentation(file.absolutePath)
                presentation.save(pdfPath, com.aspose.slides.SaveFormat.Pdf)
            }
        } catch (e: Exception) {
            LogUtils.log("PPT 转 Pdf失败", e)
        }
        return pdfPath
    }

    fun pdf2Jpg(file: File, page: Int = 0): String {
        val imgPath = file.absolutePath.replace(file.extension, "jpg")
        try {
            timedTask("Pdf 转 Jpg") {
                val document = Loader.loadPDF(file)
                val renderer = PDFRenderer(document)
                val image: BufferedImage = renderer.renderImageWithDPI(page, 72F, ImageType.RGB)
                ImageIOUtil.writeImage(image, imgPath, 72)
            }
        } catch (e: Exception) {
            LogUtils.log("pdf 转 jpg失败", e)
        }
        return imgPath
    }


    private fun timedTask(taskeName: String, block: () -> Unit) {
        val old = System.currentTimeMillis()
        block()
        val now = System.currentTimeMillis();
        LogUtils.log("$taskeName 共耗时：" + ((now - old) / 1000.0) + "秒");
    }
}
